/*******************************************************************
 * 
 *  Copyright C 2005 by Amlogic, Inc. All Rights Reserved.
 *
 *  Description: Configuration Management
 *
 *  Author: Amlogic Software
 *  Created: 3/29/2006 4:41PM
 *
 *******************************************************************/

//#include "lwip/opt.h"
//#include "lwip/def.h"
//#include <netinet/in.h>

#include "user.h"
#include "../menu.h"
#include "chipsupport.h"
#include "AVReg.h"
#include "aw_config.h"

#include "i2c_communication.h"

#define FALSE 0
#define TRUE 1
#define INIT_ADDR       0x0

#ifdef ENABLE_BLUETOOTH
#include <Drivers/bluetooth/bt_customer.h>
#include "aw_bluetooth.h"
#endif

#ifdef ENABLE_NETWORK
#include "aw_network.h"

//struct aml_iw_config     aml_wireless_config;
//struct aml_netif_setting aml_network_config;

//u8_t yahoo_auto_enable;
//u8_t google_auto_enable;
//u8_t framechannel_auto_enable;

//yahoo_login_info_t			yahoo_config;
//google_login_info_t			google_config;
framechannel_login_info_t	framechannel_config;
#endif /* ENABLE_NETWORK */
#define AML_CONFIG_VERSION	0xB3

#if 0
#define LANGUAGE_ADDR       0x1
#define MULTI_DISPLAY_ADDR  0x2
#define JPEG_PLAY_MODE_ADDR 0x3
#define SLIDE_MODE_ADDR     0x4
#define TVOUT_MODE_ADDR	 	0x5

#else

#define LANGUAGE_ADDR       0x11
#define SLIDE_DURATION_ADDR 0x12
#define SLIDE_MODE_ADDR     0x13
#define MUSIC_REPEAT_ADDR   0x14
#define MOVIE_REPEAT_ADDR	0x15
#define DISPLAY_MODE_ADDR	0x16
#define TVOUT_MODE_ADDR	 	0x17
#define JPEG_PLAY_MODE_ADDR 0x18
#define SOUND_LEVEL_ADDR	0x19
#ifdef AUTO_PLAY
#define AUTO_RUN_ADDR		0X20
#endif
#endif
#ifdef ENABLE_BLUETOOTH
#define BT_PIN_CODE_ADDR	        (0x20+16)
#define BT_LINK_KEY_ADDR	        (BT_PIN_CODE_ADDR+MAX_LINK_KEY_NUM*sizeof(bt_link_key_t))
#endif
#ifdef ENABLE_NETWORK
#define NET_MENURSS_ADDR			AUTO_RUN_ADDR
#if 0
#define NET_ENABLE_ADDR             (NET_MENURSS_ADDR + 1+5)
#define NETIF_CONFIG_ADDR           (NET_ENABLE_ADDR+1)
#define WIFI_CONFIG_ADDR            (NETIF_CONFIG_ADDR + sizeof(aml_network_config))
#define YAHOO_AUTO_ENABLE_ADDR		(WIFI_CONFIG_ADDR+sizeof(aml_wireless_config))
#define GOOGLE_AUTO_ENABLE_ADDR		(YAHOO_AUTO_ENABLE_ADDR + 1)
#define YAHOO_CONFIG_ADDR			(GOOGLE_AUTO_ENABLE_ADDR + 1)
#define GOOGLE_CONFIG_ADDR			(YAHOO_CONFIG_ADDR + sizeof(yahoo_config))
#define FRAMECHANNEL_CONFIG_ADDR	(GOOGLE_CONFIG_ADDR + sizeof(google_config))
#endif
#define NET_ENABLE_ADDR             (NET_MENURSS_ADDR + 1)
#define NETIF_CONFIG_ADDR           (NET_ENABLE_ADDR+1)
#define WIFI_CONFIG_ADDR            (NETIF_CONFIG_ADDR + sizeof(aml_network_config))
#define YAHOO_AUTO_ENABLE_ADDR		(WIFI_CONFIG_ADDR+sizeof(aml_wireless_config))
#define GOOGLE_AUTO_ENABLE_ADDR		(YAHOO_AUTO_ENABLE_ADDR + 1)
#define YAHOO_CONFIG_ADDR			(GOOGLE_AUTO_ENABLE_ADDR + 1)
#define GOOGLE_CONFIG_ADDR			(YAHOO_CONFIG_ADDR + sizeof(yahoo_config))
#define FRAMECHANNEL_CONFIG_ADDR	(GOOGLE_CONFIG_ADDR + sizeof(google_config))
#define FRAMECHANNEL_AUTO_ENABLE_ADDR	(FRAMECHANNEL_CONFIG_ADDR + sizeof(framechannel_config))
#endif /* ENABLE_NETWORK */
#ifdef RESUME_PLAY
#define RESUME_FLAG_ADDR           0x21
#define RESUME_FILETIME_ADDR       0x22
#define RESUME_FILENAME_ADDR		0x28    //RESUME_FILETIME_ADDR+5
#endif
extern int  Musicrepeat_mode_flag_extern=0;
extern int  Movierepeat_mode_flag_extern=0;
int aw_config_init()
{
#if 0
    avreg_eeprom_config_t reg_config;
	eeprom_talk = InitEeprom24c02Device();
    if (!eeprom_talk) {
        return FALSE;
    }
    reg_config.talk = eeprom_talk;
    reg_config.start_adr = 6; //6
    reg_config.max_size = 256;   

    return AVRegistryInit(AVREG_SRC_EEPROM, &reg_config);
#else
    return 0;
#endif	    
}

INT8U GetParaInit()
{
    if(I2C_device_fd < 0)
        Init_I2C_parameters();

    INT8U Init_flag;
    int i2c_rw_flag = -1;
    i2c_rw_flag = i2c_read_eeprom( (unsigned char*)&Init_flag, 1 ,INIT_ADDR);

    if(Init_flag==AML_CONFIG_VERSION)
        return 1;
    else
        return 0;
}
#if 0 //def ENABLE_NETWORK_BASIC

unsigned char SetFrameChannelConfig(framechannel_login_info_t *login)
{
	return i2c_write_eeprom((unsigned char*)login, sizeof(*login), FRAMECHANNEL_CONFIG_ADDR);
}

unsigned char SetFrameChannelAutoEnable(unsigned char enable)
{
	return i2c_write_eeprom((unsigned char *)&enable, 1, FRAMECHANNEL_AUTO_ENABLE_ADDR);
}

#ifndef YAHOO_GUEST_MODE_ONLY

//#define FLICKR_FULL_TOKEN_FILENAME "/mnt/C/flickr_full_token.cfg"
#define FLICKR_FULL_TOKEN_PATH "/mnt/C/"
#define FLICKR_FULL_TOKEN_LEN_MAX 64
unsigned char flickr_full_token[FLICKR_FULL_TOKEN_LEN_MAX+1];

char* construct_full_token_filename(char* short_token)
{
    char* buf;
    int len=strlen(FLICKR_FULL_TOKEN_PATH)+strlen(short_token)+1;
    buf=AVMem_malloc(len);
    if(buf){
        strcpy(buf,FLICKR_FULL_TOKEN_PATH);
        strcat(buf,short_token);
        buf[len-1]=0;    
    }        
    return buf;
}    

unsigned char *GetFlickrFullToken(unsigned char* short_token)
{
    int fd;
    char* filename;
    
    filename=construct_full_token_filename(short_token);
    if(filename){
    memset((unsigned char*)&flickr_full_token[0],0,FLICKR_FULL_TOKEN_LEN_MAX);
        fd=open(filename,O_RDONLY);
        AVMem_free(filename);
    if(fd>=0){
        read(fd,(unsigned char*)&flickr_full_token[0],FLICKR_FULL_TOKEN_LEN_MAX);
        flickr_full_token[FLICKR_FULL_TOKEN_LEN_MAX] = '\0';
        close(fd);
        return flickr_full_token;
    }
    else{
        return NULL;
    }
}
    return NULL;
}

void SetFlickrFullToken(unsigned char *full_token)
{ 
    int fd;
    char* filename;
    if (full_token==NULL)
        return;
    filename=construct_full_token_filename(yahoo_config.username);
    if(filename){
        fd=open(filename,O_RDWR);
    if(fd<0){
            fd = creat(filename, O_RDWR);
        if (fd < 0) {
                unlink(filename) ; 
        }
    }            
    
    memset(flickr_full_token, 0, FLICKR_FULL_TOKEN_LEN_MAX+1);
        strncpy((char*)flickr_full_token, (char*)full_token, FLICKR_FULL_TOKEN_LEN_MAX);

    if(fd>=0){
	lseek(fd,0,SEEK_SET);	    
        write(fd,flickr_full_token,FLICKR_FULL_TOKEN_LEN_MAX);
        close(fd);
    }        	
        AVMem_free(filename);       	
    }    
}

#endif


#endif

 #ifdef ENABLE_NETWORK_BASIC
/*return 0/1/2/3 
  0-scan,1-Manual,2-disable,3-enable*/
unsigned char  GetNetEnablePara(void)
{
	unsigned char enable_net_flag=0;
	i2c_read_eeprom((unsigned char*)&enable_net_flag, 1, NET_ENABLE_ADDR) ;
	return enable_net_flag;
}

void SetNetEnablePara(unsigned char enable_flag)
{
	i2c_write_eeprom((unsigned char*)&enable_flag, 1, NET_ENABLE_ADDR);
}



INT8U GetMenuNetRssPara(void)
{
	INT8U NetRss_flag=0;
	i2c_read_eeprom((unsigned char*)&NetRss_flag, 1, NET_MENURSS_ADDR) ;
	return NetRss_flag;
}

int SetMenuNetRssPara(INT8U NetRss_flag)
{
	 i2c_write_eeprom((unsigned char*)&NetRss_flag, 1, NET_MENURSS_ADDR) ;
}
#endif
void SetParaInit()
{
    if(I2C_device_fd < 0)
        Init_I2C_parameters();

    INT8U Init_flag=AML_CONFIG_VERSION;
    int i2c_rw_flag = -1;
	i2c_rw_flag = i2c_write_eeprom((unsigned char*)&Init_flag , 1 ,INIT_ADDR);
}

void ClearParaInit()
{
    unsigned char Init_flag[256]={0};//24c02 only can store 256 bytes 
    memset(&Init_flag, 0, sizeof(Init_flag));

    int i2c_rw_flag = -1;
	i2c_rw_flag = i2c_write_eeprom((unsigned char*)&Init_flag , 256 ,INIT_ADDR);
}


INT8U GetLanguagePara(void)
{
    INT8U Language_flag=1;
    int i2c_rw_flag = -1;
    i2c_rw_flag = i2c_read_eeprom( (unsigned char*)&Language_flag, 1 ,LANGUAGE_ADDR);

    return Language_flag;
}

void SetLanguagePara(INT8U language)
{
    int i2c_rw_flag  = -1;   
    i2c_rw_flag = i2c_write_eeprom((unsigned char*)&language, 1 ,LANGUAGE_ADDR);
}



INT8U GetSlideDurPara(void)
{
    INT8U Slide_Duration_flag = 0;
    int i2c_rw_flag = -1;
    i2c_rw_flag = i2c_read_eeprom( (unsigned char*)&Slide_Duration_flag, 1 ,SLIDE_DURATION_ADDR);

	return Slide_Duration_flag;
}

void SetSlideDurPara(INT8U slide_duration)
{
    int i2c_rw_flag  = -1;   
    i2c_rw_flag = i2c_write_eeprom((unsigned char*)&slide_duration, 1 ,SLIDE_DURATION_ADDR);
}


INT8U GetSlideModePara(void)
{
    INT8U Slide_Mode_flag=0;
    int i2c_rw_flag = -1;   
    i2c_rw_flag = i2c_read_eeprom( (unsigned char*)&Slide_Mode_flag, 1 ,SLIDE_MODE_ADDR);

    return Slide_Mode_flag;
}

void SetSlideModePara(INT8U slide_mode)
{
    int i2c_rw_flag  = -1;   
    i2c_rw_flag = i2c_write_eeprom((unsigned char*)&slide_mode, 1 ,SLIDE_MODE_ADDR);
}


INT8U GetMusicRepeatModePara(void)
{
    INT8U Musicrepeat_mode_flag=0;
	Musicrepeat_mode_flag=Musicrepeat_mode_flag_extern;
    int i2c_rw_flag = -1;
    i2c_rw_flag = i2c_read_eeprom( (unsigned char*)&Musicrepeat_mode_flag, 1 ,MUSIC_REPEAT_ADDR);

    return Musicrepeat_mode_flag;
}

void SetMusicRepeatModePara(INT8U musicrepeat_mode)
{
    Musicrepeat_mode_flag_extern=musicrepeat_mode;
    int i2c_rw_flag = -1;
    i2c_rw_flag = i2c_write_eeprom((unsigned char*)&musicrepeat_mode, 1 ,MUSIC_REPEAT_ADDR);
}

INT8U GetMovieRepeatModePara(void)
{
    INT8U movierepeat_mode_flag=0;
    movierepeat_mode_flag=Movierepeat_mode_flag_extern;

    int i2c_rw_flag = -1;   
    i2c_rw_flag = i2c_read_eeprom( (unsigned char*)&movierepeat_mode_flag, 1 ,MOVIE_REPEAT_ADDR);
 
    return movierepeat_mode_flag;
}

void SetMovieRepeatModePara(INT8U movierepeat_mode)
{
    Movierepeat_mode_flag_extern=movierepeat_mode;

    int i2c_rw_flag  = -1;   
    i2c_rw_flag = i2c_write_eeprom((unsigned char*)&movierepeat_mode, 1 ,MOVIE_REPEAT_ADDR);
}

INT8U GetDisplayModePara(void)
{
    INT8U display_mode_flag=0;
    int i2c_rw_flag = -1;   
    i2c_rw_flag = i2c_read_eeprom( (unsigned char*)&display_mode_flag, 1 ,DISPLAY_MODE_ADDR);

    return display_mode_flag;
}

void SetDisplayModePara(INT8U display_mode)
{
    int i2c_rw_flag = -1;   
    i2c_rw_flag = i2c_write_eeprom((unsigned char*)&display_mode, 1 ,DISPLAY_MODE_ADDR);
}

INT8U GetTVOutPutModePara(void)
{
    INT8U TVOutPutMode_flag=0;
    int i2c_rw_flag = -1;   
	i2c_rw_flag = i2c_read_eeprom((unsigned char*)&TVOutPutMode_flag, 1 ,TVOUT_MODE_ADDR);

    return TVOutPutMode_flag;;
}

void SetTVOutPutModePara(INT8U OutPutmode)
{
    int i2c_rw_flag = -1;   
	i2c_rw_flag = i2c_write_eeprom((unsigned char*)&OutPutmode, 1 ,TVOUT_MODE_ADDR);
}

INT8U GetAutoRunPara(void)
{
#ifdef AUTO_PLAY
    INT8U AutoRun_flag=0;
    int i2c_rw_flag = -1;
    i2c_rw_flag = i2c_read_eeprom( (unsigned char*)&AutoRun_flag, 1 ,AUTO_RUN_ADDR);

	return AutoRun_flag;
#else
	return 1;
#endif
}

void SetAutoRunPara(INT8U autorun)
{
#ifdef AUTO_PLAY
    int i2c_rw_flag = -1;
    i2c_rw_flag = i2c_write_eeprom((unsigned char*)&autorun, 1 ,AUTO_RUN_ADDR);
#endif
}
#ifdef RESUME_PLAY
void GetResumeFilePara(char *file_info)
{
	int i2c_rw_flag = -1;   
	i2c_rw_flag = i2c_read_eeprom((unsigned char*)file_info,RESUME_FILENAME_LENGTH,RESUME_FILENAME_ADDR);

}

void SetResumeFilePara(char *file_info)
{
    int i2c_rw_flag = -1;   
	i2c_rw_flag = i2c_write_eeprom((unsigned char*)file_info,RESUME_FILENAME_LENGTH,RESUME_FILENAME_ADDR);
}
INT32U  GetResumeTimePara(void)
{
	INT32U resume_filetime = 0;
	int i2c_rw_flag = -1;   
	i2c_rw_flag = i2c_read_eeprom((unsigned char*)&resume_filetime,4,RESUME_FILETIME_ADDR);
	return resume_filetime;
}

void SetResumeTimePara(INT32U resume_filetime)
{
    int i2c_rw_flag = -1;   
	i2c_rw_flag = i2c_write_eeprom((unsigned char*)&resume_filetime,4 ,RESUME_FILETIME_ADDR);
}

INT8U GetResumePlayFlagPara(void)
{
    INT8U resumeplayflag = 0;
    int i2c_rw_flag = -1;  
    i2c_rw_flag = i2c_read_eeprom( (unsigned char*)&resumeplayflag, 1 ,RESUME_FLAG_ADDR);

	return resumeplayflag;
}

void SetResumePlayFlagPara(INT8U resumeplayflag)
{
	int i2c_rw_flag = -1;  
	i2c_rw_flag = i2c_write_eeprom((unsigned char*)&resumeplayflag, 1 ,RESUME_FLAG_ADDR);
}

#endif

INT8U GetJpegPlayModePara(void)
{
    INT8U Jpeg_Play_Mode_flag=0;
    int i2c_rw_flag = -1;  
    i2c_rw_flag = i2c_read_eeprom( (unsigned char*)&Jpeg_Play_Mode_flag, 1 ,JPEG_PLAY_MODE_ADDR);

	return Jpeg_Play_Mode_flag;
}

void SetJpegPlayModePara(INT8U playmode)
{
	int i2c_rw_flag = -1;  
	i2c_rw_flag = i2c_write_eeprom((unsigned char*)&playmode, 1 ,JPEG_PLAY_MODE_ADDR);
}


unsigned char GetSoundLevelPara(void)
{
    unsigned char Sound_level_index = 4 ;
    int i2c_rw_flag = -1;  
    i2c_rw_flag = i2c_read_eeprom( (unsigned char*)&Sound_level_index, 1 ,SOUND_LEVEL_ADDR);

	if((Sound_level_index>0)&&(Sound_level_index<8))
		return Sound_level_index;
	else
		return 4 ;	
}

void SetSoundLevelPara(INT8U Sound_level_index)
{
    int i2c_rw_flag = -1;  
	i2c_rw_flag = i2c_write_eeprom((unsigned char*)&Sound_level_index, 1 ,SOUND_LEVEL_ADDR);
}

#ifdef ENABLE_BLUETOOTH
void GetBtPinCode(void)
{
	char* pin;
	
	BtSendDevCmd(BT_IOCMD_GET_PIN_CODE,&pin);
	
    int i2c_rw_flag = -1;  
    i2c_rw_flag = i2c_read_eeprom( (unsigned char*)&pin, 1 ,BT_LINK_KEY_ADDR);
}

void SetBtPinCode(void)
{
	char* pin;
	
	BtSendDevCmd(BT_IOCMD_GET_PIN_CODE,&pin);
	int i2c_rw_flag = -1;  
	i2c_rw_flag = i2c_write_eeprom((unsigned char*)&pin, 1 ,BT_LINK_KEY_ADDR);
}

void SetBtPinCodeByCustomer(char* customer_pin)
{
	char* pin;
	
	BtSendDevCmd(BT_IOCMD_GET_PIN_CODE,&pin);
	
	memcpy(pin,customer_pin,16);
}

void SetBtDefaultPinCode(void)
{
	char default_pin[16] = "0000";
	char* pin;
	
	BtSendDevCmd(BT_IOCMD_GET_PIN_CODE,&pin);
	memcpy(pin,&default_pin[0],16);
}

void GetBtLinkKey(void)
{
	char* key = GetBtLinkKeyAddr();
	
	int i2c_rw_flag = -1;  
    i2c_rw_flag = i2c_read_eeprom( (unsigned char*)key, (MAX_LINK_KEY_NUM*sizeof(bt_link_key_t)+1) ,BT_LINK_KEY_ADDR);
}

void SetBtLinkKey(void)
{
	char* key = GetBtLinkKeyAddr();
	char cur_num = *key;
	char* key_addr = key + ((cur_num>0)?(cur_num-1):(MAX_LINK_KEY_NUM-1))*sizeof(bt_link_key_t) + 1;
	unsigned i2c_addr = BT_LINK_KEY_ADDR + ((cur_num>0)?(cur_num-1):(MAX_LINK_KEY_NUM-1))*sizeof(bt_link_key_t) + 1;

    int i2c_rw_flag = -1;   
    i2c_rw_flag = i2c_write_eeprom((unsigned char*)key, 1 ,BT_LINK_KEY_ADDR);
    i2c_rw_flag = i2c_write_eeprom((unsigned char*)key_addr, sizeof(bt_link_key_t) ,i2c_addr);
}
#endif
#ifdef ENABLE_NETWORK
/*
char* get_par_from_external_disk(INT8U type)
{
    int len,i;
    INT8U fstype;
    DIR     *pDirID;
    struct dirent   *pDirEntry;
    int fd;
    char* tempbuf;
    static char* buf=NULL;
    char* cfg_file_name[2]={
        "hbrowser.cfg",
        ""};
    if(buf){
        AVMem_free(buf);
        buf=NULL;
    }        
    tempbuf=(char*)AVMem_malloc(128);
    if(tempbuf==NULL)
        return NULL;
    pDirID = opendir("/mnt");
    while ((pDirEntry = readdir(pDirID)) != NULL) {
        fd=-1;
        sprintf(tempbuf,"%s/%s/%s","/mnt",pDirEntry->d_name,cfg_file_name[type]);
        FileNavGetFileSystemInfo(tempbuf, NULL, &fstype,NULL);
        if(fstype==AV_FAT_FS)
            fd=open(tempbuf,O_RDONLY);
        if(fd>=0){
            len=lseek(fd,0,SEEK_END);
            lseek(fd,0,SEEK_SET);
            buf=(char*)AVMem_malloc(len);
            if(buf){
                read(fd,buf,len);
                for(i=0;i<len;i++){
                    if((buf[i]==0x0d)||(buf[i]==0x0a))
                        buf[i]=0;   
                }    
            }
            close(fd);
            break;
        }
    }//end while
    closedir(pDirID); 
    AVMem_free(tempbuf);  
    
    return buf;   
}
*/
#endif


